package com.ljkj.blockpay.network.response;

import android.text.TextUtils;

import com.ljkj.blockpay.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class DisposableDataCallBack<T> extends AbstractDisposableCallBack<BaseDataResponse<T>> {

    @Override
    public void onSuccess(BaseDataResponse<T> baseResponse) {
        if (!TextUtils.equals(baseResponse.code, HttpErrorCode.SUCCESS)) {
            onFailed(baseResponse.code, baseResponse.msg);
            return;
        }
        if (baseResponse.data == null) {
            onFailed(HttpErrorCode.EMPTY, "");
            return;
        }

        if (CollectionUtils.isEmpty(baseResponse.data)) {
            onFailed(HttpErrorCode.EMPTY, "");
            return;
        }
        // 去空
        ArrayList<T> newList = new ArrayList<>(baseResponse.data.size());
        for (T t : baseResponse.data) {
            if (t == null) {
                continue;
            }
            newList.add(t);
        }

        onSafeSuccess(newList);
    }

    protected void onSafeSuccess(List<T> list) {
        // 推荐调用该方法
    }

    @Override
    protected void onSafeFailed(String code, String message) {
    }
}
